﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ExaminationTT.Model;
using ExaminationTT.Control;
using ConcreteMappersAndObjects.DomainObjects;
using ExaminationTT.Utils;

namespace ExaminationTT.View
{
    public partial class ExamRoomDefiner : Form
    {
        private IExam _exam;
        private BindingList<IRoom> _availableRooms;
        private BindingList<IRoom> _selectedRooms;
        

        private int _occupiedSeats;
        private int _seatsToOccupie;
        private HostApplication _host;

        public ExamRoomDefiner(IExam exam, HostApplication host)
        {
            _host = host;
            _exam = exam;
            InitializeComponent();
            _selectedRooms = new BindingList<IRoom>();
            _availableRooms = new BindingList<IRoom>();
            _lBox_availableRooms.DataSource = _availableRooms;
            _lBox_selectedRooms.DataSource = _selectedRooms;
            _lBox_availableRooms.DisplayMember = "Description";
            _lBox_selectedRooms.DisplayMember = "Description";
            IEnumerable<IRoom> rooms = _host.GetAllRooms();
            foreach (IRoomExam re in _exam.Rooms)
            {
                _selectedRooms.Add(re.Room);
            }
            List<IRoom> roomsNotAvailable = new List<IRoom>();
            foreach (IRoomExam rexam in _host.GetAllReservedRooms())
            {
                if (Utilities.ExamIntersects(rexam.Exam, _exam))
                    roomsNotAvailable.Add(rexam.Room);
            }
            IEnumerable<IRoom> availableRooms = rooms.Except(roomsNotAvailable);
            availableRooms = availableRooms.Except(_selectedRooms);

            foreach (IRoom r in availableRooms)
            {
                _availableRooms.Add(r);
            }

            _txtBox_examInfo.Text = String.Format("Exame de {0} marcado para dia {1} com início às {2}, com uma duração de {3} minutos"
                , _exam.CUnit.Acronym, _exam.ExamDate.ToShortDateString(), _exam.ExamDate.ToShortTimeString(), ((int)_exam.ExamEndDate.Subtract(_exam.ExamDate).TotalMinutes).ToString());
            _txtBox_subscriptionInfo.Text = String.Format("Exame com {0} estudantes inscritos", _exam.CUnit.Subscriptions.Count());
            _occupiedSeats = 0;
            foreach (var item in _exam.Rooms)
            {
                _occupiedSeats += item.Room.Seats;
            }
            _seatsToOccupie = _exam.CUnit.Subscriptions.Count() - _occupiedSeats < 0 ? 0 : _exam.CUnit.Subscriptions.Count() - _occupiedSeats;
            UpdateTextBoxes();
        }

        private void _btn_add_Click(object sender, EventArgs e)
        {
            Room item = _lBox_availableRooms.SelectedItem as Room;
            if (item != null)
            {
                _host.SetExamRoom(_exam, item);;
                _availableRooms.Remove(item);
                _selectedRooms.Add(item);
                _lBox_availableRooms_SelectedIndexChanged(null, null);
                _lBox_selectedRooms_SelectedIndexChanged(null, null);
                _occupiedSeats += item.Seats;
                _seatsToOccupie -= item.Seats;
                UpdateTextBoxes();
            }
        }

        private void _btn_remove_Click(object sender, EventArgs e)
        {
            IRoom item = _lBox_selectedRooms.SelectedItem as Room;
            if (item != null)
            {
                _host.RemoveExamRoom(_exam, item);// _exam.RemoveRoom(item);
                _availableRooms.Add(item);
                _selectedRooms.Remove(item);
                _lBox_availableRooms_SelectedIndexChanged(null, null);
                _lBox_selectedRooms_SelectedIndexChanged(null, null);
                _occupiedSeats -= item.Seats;
                _seatsToOccupie += item.Seats;
                UpdateTextBoxes();
            }
        }

        private void _lBox_availableRooms_SelectedIndexChanged(object sender, EventArgs e)
        {
            IRoom room = _lBox_availableRooms.SelectedItem as IRoom;
            if (room == null) { _txtBox_availableSeatsInfo.Text = string.Empty; return; }
            _txtBox_availableSeatsInfo.Text = room.Seats.ToString();
        }

        private void _lBox_selectedRooms_SelectedIndexChanged(object sender, EventArgs e)
        {
            IRoom room = _lBox_selectedRooms.SelectedItem as IRoom;
            if (room == null) { _txtBox_selectedSeatsInfo.Text = string.Empty; return; }
            _txtBox_selectedSeatsInfo.Text = room.Seats.ToString();
        }

        private void UpdateTextBoxes()
        {
            _txtBox_ocuppiedSeats.Text = _occupiedSeats.ToString();
            if (_seatsToOccupie < 0)
                _txtBox_seatsToOccupie.Text = "0";
            else if (_seatsToOccupie > _exam.CUnit.Subscriptions.Count())
                _txtBox_seatsToOccupie.Text = _exam.CUnit.Subscriptions.Count().ToString();
            else
                _txtBox_seatsToOccupie.Text = _seatsToOccupie.ToString();
        }
    }
}
